package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Depot;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepotService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 丧彪
 */
@Controller
@RequestMapping("depot")
public class DepotController {
    @Autowired
    private IDepotService depotService;
    
    @RequiresPermissions("depot:index")
    @PermissionName("仓库首页")
    @RequestMapping("")
    public String index(){
        return "depot/list";
    }

    @RequiresPermissions("depot:list")
    @PermissionName("仓库列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        return depotService.query(qo);
    }
    
    @RequiresPermissions("depot:save")
    @PermissionName("仓库保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Depot entity){
    	JsonResult result = new JsonResult();
    	result.markMsg("保存成功");
    	try {
    		depotService.save(entity);
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("保存失败" + e.getMessage());
		}
        return result;
    }
    
    @RequiresPermissions("depot:update")
    @PermissionName("仓库更新")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Depot entity){
    	JsonResult result = new JsonResult();
    	try {
    		depotService.update(entity);
    		result.markMsg("编辑成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("更新失败" + e.getMessage());
		}
        return result;
    }

    @RequestMapping("changeState")
    @ResponseBody
    public JsonResult changState(Long id){
        JsonResult result = new JsonResult();
        try {
            depotService.changState(id);
            result.markMsg("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("操作失败");
        }
        return result;
    }
    
    
    @RequestMapping("queryDepots")
    @ResponseBody
    public List<Depot> queryDepots(){
        return depotService.list();
    }
}
